/***
  author: sailing
  date: 2016/11/16
**/
import axios from 'axios'
import tools from './tools'
import eventBus from '../eventBus'
export default {
  getJson (config) {
    const url = tools.mergeUrl(config.url)
    eventBus.bus.$emit('showLoading')
    axios.get(url)
      .then(function (response) {
        eventBus.bus.$emit('hideLoading')
        config.callback.call(config.self, response.data)
      })
      .catch(function (error) {
        eventBus.bus.$emit('hideLoading')
        console.log('get error:', error)
      })
  },

  postJson (config) {
    /*
    axios.post(config.url, config.data)
      .then(function (response) {
        eventBus.bus.$emit('hideLoading')
        config.callback.call(config.self, response.data)
      })
      .catch(function (error) {
        eventBus.bus.$emit('hideLoading')
        console.log('get error:', error)
      })
    */
  
    $.ajax({
      url: config.url,
      type: 'POST',
      beforSend: function () {
        // 禁用按钮防止重复提交
        // $(smjObj.selector).attr({disabled: "disabled"});
      },
      data: config.data,
      success: function (res) {
        config.callback.call(config.self, res.data)
      },
      error: function (msg) {
        // console.log(msg)
      },
      complete: function () {
      }
    })
    
/*
    axios({
      method: 'post',
      url: config.url,
      headers: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      data: config.data
    })
    .then(function (response) {
      config.callback.call(config.self, response.data)
    })
    */
  }
}
